package com.ruoyi.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.customer.domain.TMesCustomer;
import com.ruoyi.customer.mapper.TMesCustomerMapper;
import com.ruoyi.customer.service.ITMesCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class TMesCustomerServiceImpl extends ServiceImpl<TMesCustomerMapper, TMesCustomer> implements ITMesCustomerService {
    @Autowired
    private TMesCustomerMapper tMesCustomerMapper;

    /**
     * 查询客户
     *
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public TMesCustomer selectTMesCustomerById(Long id) {
        return tMesCustomerMapper.selectTMesCustomerById(id);
    }

    /**
     * 查询客户列表
     *
     * @param tMesCustomer 客户
     * @return 客户
     */
    @Override
    public List<TMesCustomer> selectTMesCustomerList(TMesCustomer tMesCustomer) {
        return tMesCustomerMapper.selectTMesCustomerList(tMesCustomer);
    }

    /**
     * 新增客户
     *
     * @param tMesCustomer 客户
     * @return 结果
     */
    @Override
    public TMesCustomer insertTMesCustomer(TMesCustomer tMesCustomer) {
        //  tMesCustomerMapper.insertTMesCustomer(tMesCustomer);
        save(tMesCustomer);
        return tMesCustomer;
    }

    /**
     * 修改客户
     *
     * @param tMesCustomer 客户
     * @return 结果
     */
    @Override
    public TMesCustomer updateTMesCustomer(TMesCustomer tMesCustomer) {
        //  tMesCustomerMapper.updateTMesCustomer(tMesCustomer);
        saveOrUpdate(tMesCustomer);
        return tMesCustomer;
    }

    /**
     * 批量删除客户
     *
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteTMesCustomerByIds(Long[] ids) {
        return tMesCustomerMapper.deleteTMesCustomerByIds(ids);
    }

    /**
     * 条件删除
     * @param tMesCustomer 客户
     * @return
     */
    @Override
    public int delete(TMesCustomer tMesCustomer) {
        QueryWrapper<TMesCustomer> qw = new QueryWrapper<>();
        QueryTool.condition(qw, tMesCustomer);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param tMesCustomer 客户
     * @return
     */
    @Override
    public Pages<TMesCustomer> queryPage(TMesCustomer tMesCustomer) {
        QueryWrapper<TMesCustomer> qw = new QueryWrapper<>();
        QueryTool.condition(qw, tMesCustomer);
        IPage<TMesCustomer> ipage = new Query<TMesCustomer>().getPage(tMesCustomer);

        IPage<TMesCustomer> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public String generNo(String prefix, String tbName) {
        int num = tMesCustomerMapper.getTodayNum(tbName);
        num += 1;
        String orderNo = prefix + "_" + DateUtils.dateTimeNow("yyyyMMdd")  + String.format("%04d", num);
        return orderNo;
    }

    @Override
    public String generNo2(String prefix, String split, String tbName) {
        Long maxId = tMesCustomerMapper.getMaxId(tbName);

        if (maxId == null) {
            maxId = 0L;
        }
        maxId += 1;

        String no = prefix + split + String.format("%05d", maxId);

        return no;
    }


    /**
     * 删除客户信息
     *
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteTMesCustomerById(Long id) {
        return tMesCustomerMapper.deleteTMesCustomerById(id);
    }
}
